<template>
  <el-dialog :title="title" :visible.sync="openDialog" @close="close">
    <el-row type="flex" justify="center">
      <el-col :span="18">
        <el-form ref="addform" :model="form" label-suffix="：" :rules="rules" label-position="right" label-width="120px">
          <el-form-item label="品牌名称" prop="name">
            <el-input v-model="form.name" autocomplete="off" />
          </el-form-item>
          <el-form-item label="品牌首字母" prop="first_name">
            <el-input v-model="form.first_name" autocomplete="off" />
          </el-form-item>
          <el-form-item label="品牌LOGO" prop="PicUrl">
            <image-upload ref="upimg" v-model="form.img" :width="314" :height="168" />
          </el-form-item>
          <el-form-item label="供应商" prop="shop_id">
            <el-select v-model="form.shop_id" placeholder="请选择">
              <el-option v-for="item in GYSoptions" :key="item.id" :label="item.name" :value="item.id" />
            </el-select>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>

    <div slot="footer" class="dialog-footer">
      <el-button @click="openDialog = false">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { api_getDataList, api_addAndUpdate } from '@/api/query'
export default {
  data() {
    return {
      id: '',
      title: '新增品牌信息',
      openDialog: false,
      GYSoptions: [],
      form: {
        name: '',
        first_name: '',
        shop_id: '',
        img: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入品牌名称', trigger: 'blur' },
          { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
        ],
        first_name: [
          { pattern: /^[A-Z]{1}$/, message: '只能输入A-Z一位大写字母', trigger: 'change' }
        ],
        img: [
          { required: true, message: '必须上传品牌Logo', trigger: 'change' }
        ],
        shop_id: [
          { required: true, message: '请选择品牌供应商', trigger: 'change' }
        ]

      }

    }
  },
  computed: {
    queryData() {
      const obj = this.form
      obj.id = this.id
      return obj
    }
  },
  methods: {

    // 上传完成
    uploadSuccess(url) {
      console.log(url, '测试文件上传')
      this.PicUrl = url
    },
    init(val, GYSoptions) {
      this.openDialog = true
      this.GYSoptions = GYSoptions
      if (val) {
        this.title = '修改品牌信息'
        this.id = val.id
        this.form = val

        console.log(this.form, 789)
      } else {
        this.title = '新增品牌信息'
        this.id = ''
      }
    },
    close() {
      this.$refs.addform.resetFields()
      this.form = this.$options.data().form
    },
    save() {
      this.$refs.addform.validate((valid) => {
        if (valid) {
          this.$showLoading()
          api_addAndUpdate('/brand/save', this.queryData).then((res) => {
            if (res.code === 0) {
              this.$emit('save', '提交')
              this.$showLoading('close')
              this.openDialog = false
              this.$operateTip()
            }
          })
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
</style>
